package com.student.web.student;

import com.google.gson.Gson;
import com.student.pojo.Student;
import com.student.service.student.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 分页查询 参数pageNum pageSize 返回LimitPage
 * 普通查询 参数name 返回LimitPage
 *
 * @author lin945
 * @date 2020/4/27 22:32
 */
@WebServlet("/getstudent")
public class getStudent extends HttpServlet {
    private StudentService studentService=new StudentService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");

        String id = req.getParameter("id");
        if (id != null) {
            Student s = studentService.getStudentById(id);
            resp.getWriter().write(new Gson().toJson(s));
        } else {
            int pagenum = Integer.parseInt(req.getParameter("pageNum"));
            int pageSize = Integer.parseInt(req.getParameter("pageSize"));
            String studentname = req.getParameter("studentname");
            Gson gson = new Gson();
            if (studentname == null | studentname.equals("")) {
                String resut = gson.toJson(studentService.getLimitPage((int) pagenum, (int) pageSize));
                resp.getWriter().write(resut);
            } else {
                String resut = gson.toJson(studentService.getLimitPageByName((int) pagenum, (int) pageSize, studentname));
                resp.getWriter().write(resut);
            }
            // String
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
